import path from "node:path";
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from "vite";
import vue from '@vitejs/plugin-vue'
import { ENV_PATH, outBaseDir, PUBLIC_DIR } from '../nodetools/project';
import { wrapperEnv } from '../nodetools/vite.util';

const outDir = path.join(outBaseDir, 'desktop');

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, ENV_PATH, "");
  const viteEnv = wrapperEnv(env);
  const { VITE_RENTER_DESKTOP_PORT } = viteEnv;
  return {
    plugins: [
      vue()
    ],
    base: "./",
    root: ".",
    envPrefix: "VITE_",
    envDir: ENV_PATH,
    publicDir: PUBLIC_DIR,
    server: {
      host: "127.0.0.1",
      port: VITE_RENTER_DESKTOP_PORT,
      proxy: {
        '/api': {
          target: 'http://123.249.127.238:8080',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
      
    },
    build: {
      target: "modules",
      outDir: outDir,
      assetsDir: "assets",
      sourcemap: false,
      emptyOutDir: true,
      copyPublicDir: true,
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    define: {
      __APP_ENV__: env.APP_ENV,
    },
    logLevel: "info",
  }
})

